<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>自定义模板引擎的基本原理--闫海的个人博客-芒果小叨</title>
<meta name="keywords" content="个人博客,闫海个人博客,模板引擎" />
<meta name="description" content="为了解模板引擎的工作原理， 定义一个简单模板引擎类。可以处理简单的变量替换，分支结构，数组循环遍历，模板之间的相互嵌套。" />
<link href="/Public/Index/css/base.css" rel="stylesheet">
<link href="/Public/Index/css/new.css" rel="stylesheet">
<link rel="stylesheet" href="/Public/Index/css/animate.css">
<link rel="stylesheet" type="text/css" href="/Public/Admin/ueditor/third-party/SyntaxHighlighter/shCoreDefault.css">
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script><script type="text/javascript">uaredirect("http://yanhai0531.com/mobile.php");</script>
<!--[if lt IE 9]>
<script src="/Public/Index/js/modernizr.js"></script>
<![endif]-->

<script type="text/javascript" src="/Public/Admin/Js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/Public/Admin/ueditor/third-party/SyntaxHighlighter/shCore.js"></script>
<script type="text/javascript">
  SyntaxHighlighter.all();
</script>
<script type="text/javascript">
  var url = '/index/article';
  $(function(){
    var id = '25';
    $.post(url+'/addClick',{'id':id},function(data){
        $("#pclick").html('点击数：' + data);
    });
  })
</script>
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F5a97c6527b57694be765111b6a23bffc' type='text/javascript'%3E%3C/script%3E"));
</script>
</head>
<body>
<script src="/Public/Index/js/jquery.js"></script>
<div class='header animated bounceInDown'>
    <div class='wrap'>
      <div class='logo'><a data-scroll-reveal='enter from the top over 0.7s' href='/'>
         <img src='/Public/Index/images/logo.png' title='闫海的个人博客首页' alt="logo" /></a>
      </div>
      <div data-scroll-reveal='enter from the top over 0.7s' class='top-nav'>
          <ul id="topnav">
            <li><a href='/'  title='首页'>首页<span></span></a></li>
            <li><a href='/index/list/2.html' title='关于我'>关于我<span> </span></a></li><li><a href='/index/list/3.html' title='时光轴'>时光轴<span> </span></a></li><li><a href='/index/list/1.html' title='品味人生'>品味人生<span> </span></a></li><li><a href='/index/list/5.html' title='技术分享'>技术分享<span> </span></a></li>            <li><a href='/index/article/message.html'  title='给我留言'>给我留言<span></span></a></li>
          </ul>
      </div>
      <div class='clear'></div>
    </div>
</div>
<article class="blogs">
  <h1 class="t_nav">
  	<span>您当前的位置：<a href="/index.html">首页</a>&nbsp;&gt;&nbsp;
	<a href="/index/list/5.html">技术分享</a>
	&nbsp;&gt;&nbsp;<a href="/index/list/10.html">php</a>
	
  	</span>  <a href="/index.html" class="n1">网站首页</a>
  </h1>
  <div class="index_about">
    <h2 class="c_titile">自定义模板引擎的基本原理</h2>
    <p class="box_c"><span class="d_time">发布时间：2015-01-04 15:03</span><span>编辑：小叨</span><span id="pclick">点击数：166</span></p>
    <ul class="infos" style="word-break: break-all;">
      <pre class=\"brush:php;toolbar:false\">&lt;?php
/**
&nbsp;*&nbsp;file:&nbsp;mytpl.class.php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;类名：MyTpl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;自定义模板引擎
&nbsp;*&nbsp;通过该类对象家在模板文件并解析，讲解析后的结果输出
&nbsp;*/
class&nbsp;MyTpl&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;$template_dir&nbsp;=&nbsp;&#39;templates&#39;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//定义模板文件存放目录
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;$compile_dir&nbsp;=&nbsp;&#39;templates_c&#39;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//定义通过模板引擎组合后文件存放的目录
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;$left_delimiter&nbsp;=&nbsp;&#39;&lt;{&#39;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//在模板中嵌入动态数据变量的左侧定界符
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;$right_delimiter&nbsp;=&nbsp;&#39;}&gt;&#39;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//在模板中嵌入动态数据变量的右侧定界符
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;$tpl_vars&nbsp;=&nbsp;array();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//内部使用的临时变量
&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;将PHP中分配的值保存到成员属性$tpl_vars中，用于将模板中对应的变量进行替换
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;[type]&nbsp;$tpl_var&nbsp;需要一个字符串参数作为关联数组下标，要和模板中的变量名对应
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;[type]&nbsp;$value&nbsp;&nbsp;&nbsp;需要一个标量类型的值，用来分配给模板中的变量
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;assign($tpl_var,&nbsp;$value&nbsp;=&nbsp;null){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($tpl_var&nbsp;!=&nbsp;&#39;&#39;){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;tpl_vars[$tpl_var]&nbsp;=&nbsp;$value;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;tpl_replace($content){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#将左右定界符号中，有影响正则的特殊符号转义，例如，&lt;{}&gt;转义\\&lt;\\{\\}\\&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$left&nbsp;=&nbsp;preg_quote($this-&gt;left_delimiter,&#39;/&#39;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$right&nbsp;=&nbsp;preg_quote($this-&gt;right_delimiter,&#39;/&#39;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配模板中各种标识符的正则表达式的模式数组
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$pattern&nbsp;=&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配模板中变量，例如&quot;&lt;{$var}&gt;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s*&#39;.$right.&#39;/i&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配模板中if标识符，例如&quot;&lt;{&nbsp;if&nbsp;$col&nbsp;==&nbsp;&quot;sex&quot;&nbsp;}&gt;&nbsp;&lt;{&nbsp;/if&nbsp;}&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*if\\s*(.+?)\\s*&#39;.$right.&#39;(.+?)&#39;.$left.&#39;\\s*\\/if\\s*&#39;.$right.&#39;/ies&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配elseif标识符，例如&quot;&lt;{&nbsp;elseif&nbsp;$col&nbsp;==&nbsp;&quot;sex&quot;}&gt;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*else\\s*if\\s*(.+?)\\s*&#39;.$right.&#39;/ies&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配else标识符，例如&quot;&lt;{&nbsp;else&nbsp;}&gt;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*else\\*&#39;.$right.&#39;/is&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#用来匹配模板中的loop标识符，用来便利数组中的值，例如&quot;&lt;{&nbsp;loop&nbsp;$arrs&nbsp;$value&nbsp;}&gt;&lt;{&nbsp;/loop}&gt;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*loop\\s+\\$(\\S+)\\s+\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s*&#39;.$right.&#39;(.+?)&#39;.$left.&#39;\\s*\\/loop\\s*&#39;.$right.&#39;/is&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#用来遍历数组中的键和值，例如&quot;&lt;{&nbsp;loop&nbsp;$arrs&nbsp;$key&nbsp;=&gt;&nbsp;$value&nbsp;}&gt;&nbsp;&lt;{&nbsp;/loop&nbsp;}&gt;&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*loop\\s+\\$(\\S+)\\s+\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s*=&gt;\\s*\\$(\\S+)\\s*&#39;.$right.&#39;(.+?)&#39;.$left.&#39;\\s*\\/loop\\s*&#39;.$right.&#39;/is&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配include标识符，例如，&#39;&lt;{&nbsp;include&nbsp;&quot;header.html&quot;}&gt;&#39;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;/&#39;.$left.&#39;\\s*include\\s+[\\&quot;\\&#39;]?(.+?)[\\&quot;\\&#39;]?\\s*&#39;.$right.&#39;/ie&#39;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换从模板中使用正则表达式匹配到的字符串数组&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$replacement&nbsp;=&nbsp;array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换模板中的变量&lt;?php&nbsp;echo&nbsp;$this-&gt;tpl_vars[&quot;var&quot;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;&lt;?php&nbsp;echo&nbsp;$this-&gt;tpl_vars[&quot;${1}&quot;];&nbsp;?&gt;&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换模板中的if字符串&lt;?php&nbsp;if($col&nbsp;==&nbsp;&quot;sex&quot;)&nbsp;{&nbsp;&gt;?&nbsp;&lt;?php&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;$this-&gt;stripvtags(\\&#39;&lt;?php&nbsp;if(${1})&nbsp;{&nbsp;?&gt;\\&#39;,\\&#39;${2}&lt;?php&nbsp;}&nbsp;?&gt;\\&#39;)&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换elseif的字符串&lt;?php&nbsp;}&nbsp;elseif($col&nbsp;==&quot;sex&quot;)&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;$this-&gt;stripvtags(\\&#39;&lt;?php&nbsp;}&nbsp;elseif(${1})&nbsp;{&nbsp;?&gt;\\&#39;,&quot;&quot;)&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换else的字符串
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;&lt;?php&nbsp;}&nbsp;else&nbsp;{&nbsp;?&gt;&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换模板中的loop标识符为foreach循环
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;&lt;?php&nbsp;foreach($this-&gt;tpl_vars[&quot;${1}&quot;]&nbsp;as&nbsp;$this-&gt;tpl_vars[&quot;${2}&quot;])&nbsp;{&nbsp;?&gt;${3}&lt;?php&nbsp;}&nbsp;?&gt;&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;&lt;?php&nbsp;foreach($this-&gt;tpl_vars[&quot;${1}&quot;]&nbsp;as&nbsp;$this-&gt;tpl_vars[&quot;${2}&quot;]&nbsp;=&gt;&nbsp;$this-&gt;tpl_vars[&quot;${3}&quot;])&nbsp;{&nbsp;?&gt;${4}&lt;?php&nbsp;}&nbsp;?&gt;&#39;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换include的字符串
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;file_get_contents($this-&gt;template_dir.&quot;/${1}&quot;)&#39;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#使用正则替换函数处理
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$repContent&nbsp;=&nbsp;preg_replace($pattern,&nbsp;$replacement,&nbsp;$content);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#如果还有要替换的标识，递归调用自己再次替换
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(preg_match(&#39;/&#39;.$left.&#39;([^(&#39;.$right.&#39;)]{1,})&#39;.$right.&#39;/&#39;,&nbsp;$repContent))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$repContent&nbsp;=&nbsp;$this-&gt;tpl_replace($repContent);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$repContent;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;加载指定目录下的模板文件，并将替换后的内容生成组合文件存放到另一指定目录下
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;&nbsp;[type]&nbsp;$fileName&nbsp;模板文件的文件名
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;[type]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[description]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;display($fileName){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tplFile&nbsp;=&nbsp;$this-&gt;template_dir.&#39;/&#39;.$fileName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//到指定目录中寻找模板文件
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#如果需要处理的模板文件不存在，则退出并报告错误
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!file_exists($tplFile)){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&quot;模板文件{$tplFile}不存在!&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$comFileName&nbsp;=&nbsp;$this-&gt;compile_dir.&quot;/com_&quot;.$fileName.&#39;.php&#39;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//获取组合的模板文件，该文件中的内容是被替换过的
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#判断替换后的文件是否存在或是存在但有改动，都需要重新创建
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!file_exists($comFileName)&nbsp;||&nbsp;filemtime($comFileName)&lt;filemtime($tplFile))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$repContent&nbsp;=&nbsp;$this-&gt;tpl_replace(file_get_contents($tplFile));&nbsp;&nbsp;//调用内部替换模板方法tpl_replace()。file_get_contents把文件转为字符串
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_put_contents($comFileName,&nbsp;$repContent);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//保存由系统组合后的脚本文件。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include($comFileName);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//把处理后的模板文件输出给客户端
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;stripvtags($expr,$statement=&#39;&#39;){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#匹配变量的正则
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$var_pattern&nbsp;=&nbsp;&#39;/\\s*\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s*/is&#39;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#将变量替换为值
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$expr&nbsp;=&nbsp;preg_replace($var_pattern,&nbsp;&#39;$this-&gt;tpl_vars[&quot;${1}&quot;]&#39;,&nbsp;$expr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#将开始标记中的引号转义替换
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$expr&nbsp;=&nbsp;str_replace(&quot;\\\\\\&quot;&quot;,&quot;\\&quot;&quot;,$expr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#替换语句体和结束标记中的引号
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$statement&nbsp;=&nbsp;str_replace(&quot;\\\\\\&quot;&quot;,&quot;\\&quot;&quot;,$statement);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#将处理后的条件语句相连后返回
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$sxpr.$statement;
&nbsp;&nbsp;&nbsp;&nbsp;}
}</pre><p>模板放在当前目录的templates中，编译后的放在templates_c目录下，通过include()将处理后的模板文件使用php解析后发给客户端</p>    </ul>
    <div class="keybq">
    <p><span>关键字词</span>：模板引擎</p>
    
    </div>
    <div class="ad"> </div>
    <div class="nextinfo">
      <p>上一篇：<a href="/index/article/23.html">图像处理类（缩略图，裁剪，打水印）</a></p>
      <p>下一篇：<a href="/index/article/26.html">php端验证</a></p>
    </div>
    <div class="otherlink">
      <h2>相关文章</h2>
      <ul>
        <li><a href="/index/article/44.html" title="写了一个PHP框架">写了一个PHP框架</a></li><li><a href="/index/article/35.html" title="小i机器人微信自动回复代码测试">小i机器人微信自动回复代码测试</a></li><li><a href="/index/article/26.html" title="php端验证">php端验证</a></li><li><a href="/index/article/25.html" title="自定义模板引擎的基本原理">自定义模板引擎的基本原理</a></li><li><a href="/index/article/23.html" title="图像处理类（缩略图，裁剪，打水印）">图像处理类（缩略图，裁剪，打水印）</a></li><li><a href="/index/article/19.html" title="验证码类(练手)">验证码类(练手)</a></li>        
      </ul>
    </div>
  </div>
  <aside class="right">
    <div class="news" style="margin-top:10px;">
    <h3>
	<p>最新<span>文章</span></p>
</h3>
	<ul class="rank">
		<li><a href="/index/article/49.html" title="新版网站完成50%了，继续加油吧" >新版网站完成50%了，继续加油吧</a></li><li><a href="/index/article/48.html" title="【Laravel5教程】mac+apache+mysql配置多站点(一)" >【Laravel5教程】mac+apache+mysql配置多站点(一)</a></li><li><a href="/index/article/47.html" title="Laravel5.1配置" >Laravel5.1配置</a></li><li><a href="/index/article/46.html" title="laravel5.1安装" >laravel5.1安装</a></li><li><a href="/index/article/45.html" title="初探Laravel5" >初探Laravel5</a></li><li><a href="/index/article/44.html" title="写了一个PHP框架" >写了一个PHP框架</a></li><li><a href="/index/article/42.html" title="当遇到问题时应该怎样去处理？" >当遇到问题时应该怎样去处理？</a></li><li><a href="/index/article/41.html" title="我的近况和计划" >我的近况和计划</a></li>	</ul>
<h3 class="ph">
    <p>点击<span>排行</span></p>
</h3>
	<ul class="paih">
    	<li><a href="/index/article/31.html" title="关于网站限制ie10以下浏览器访问的探讨" >关于网站限制ie10以下浏览器访问的探讨</a></li><li><a href="/index/article/46.html" title="laravel5.1安装" >laravel5.1安装</a></li><li><a href="/index/article/47.html" title="Laravel5.1配置" >Laravel5.1配置</a></li><li><a href="/index/article/45.html" title="初探Laravel5" >初探Laravel5</a></li><li><a href="/index/article/41.html" title="我的近况和计划" >我的近况和计划</a></li>	</ul>
<h3 class="links">
	<p>友情<span>链接</span></p>
</h3>
	<ul class="website">
		<li><a href="http://www.vso8.com" target="_blank">v8视频</a></li><li><a href="http://www.huanyun.net.cn" target="_blank">焕云企业管理有限公司</a></li><li><a href="http://www.sinkuo.com" target="_blank">济宁网站建设</a></li>	</ul>    </div>  
    <!-- Baidu Button BEGIN -->
    <div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare"><a class="bds_tsina"></a><a class="bds_qzone"></a><a class="bds_tqq"></a><a class="bds_renren"></a><span class="bds_more"></span><a class="shareCount"></a></div>
    <script type="text/javascript" id="bdshare_js" data="type=tools&amp;uid=6574585" ></script> 
    <script type="text/javascript" id="bdshell_js"></script> 
    <script type="text/javascript">

document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script> 
    <!-- Baidu Button END -->   
    <a onclick="javascript:window.open('/Public/Index/images/weixin.jpg','a', 'height=600,width=600,status=no,toolbar=no,menubar=no,location=no,scrollbars=no');" class="weixin"> </a></aside> 
</article>
<footer>
  <p><a href="http://www.miitbeian.gov.cn/" target="_blank">Copyright © 2015 yanhai All Rights Reversed | 鲁ICP备15004472号-1</a> </p>
</footer>
<script src="/Public/Index/js/silder.js"></script>

<script type="text/javascript">
     if(typeof(Worker) === "undefined"){
     	$.get('/Public/Html5/html5.html', function(data){
			$('body').html(data);
		})
     }
</script>

</body>
</html>